<!--添加修改角色信息-->
<template>
    <a-modal v-model="showAddTable" :mask-closable="false" :closable="false" :width="900"
             :title="title ? '新增入账' : '编辑入账'"
             :maskClosable="false"
             :destroyOnClose="true">
        <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
            <a-form-item label="订单：" prop="orderId" v-bind="formStyle" required>
              <a-select
                  placeholder="请选择订单" v-model="orderId"
                  v-decorator="['orderId', { rules: [{ required: true, message: '选择订单' }] }]"
                  @select="changeOrderList"

              >
                <a-select-option v-for="item in orderSelectList" :value="item.value"
                                 :key="item.value">{{item.label }}
                </a-select-option>
              </a-select>
            </a-form-item>
          <a-form-item label="收货时间：" prop="receiveTime" v-bind="formStyle" required>
            <a-date-picker
                format="YYYY-MM-DD HH:MM:SS"
                v-model="receiveTime"
                showTime
                placeholder="收货时间"
                style="width: 100%"
                v-decorator="['receiveTime', { rules: [{ required: true, message: '选择收货时间' }] }]"
            />
          </a-form-item>
          <a-form-item label="存放位置：" prop="location" v-bind="formStyle" required>
            <a-input
                placeholder="请输入存放位置" ref="re"
                v-decorator="[
                        'location',
                        {
                            rules: [
                                { required: true, message: '请输入汉字、字母、数字、_或-字符' },
                                { validator: this.verify.validateName.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
            />
          </a-form-item>

          <a-form-item label="在用状态：" pro="useState" v-bind="formStyle" required>
            <a-radio-group  v-model="useState" >
              <a-radio value="0">未使用</a-radio>
              <a-radio value="1">已使用</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="入账人员：" prop="entryPersion" v-bind="formStyle" required>
            <a-input
                placeholder="请输入入账人员" ref="re"
                v-decorator="[
                        'entryPersion',
                        {
                            rules: [
                                { required: true, message: '请输入汉字、字母、数字、_或-字符' },
                                { validator: this.verify.validateName.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
            />
          </a-form-item>
            <a-form-item label="入账时间：" prop="entryTime" v-bind="formStyle" required>
              <a-date-picker
                  format="YYYY-MM-DD HH:mm:ss"
                  v-model="entryTime"
                  showTime
                  placeholder="入账时间"
                  style="width: 100%"
                  v-decorator="['entryTime', { rules: [{ required: true, message: '选择入账时间' }] }]"
              />
          </a-form-item>
        </a-form>
        <div slot="footer">
            <a-button type="default" size="default" @click="cancelApply()">取消</a-button>
            <a-button type="primary" size="default" @click="saveApply()">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'
    import AFormItem from 'ant-design-vue/es/form/FormItem'
    import {validateID} from "@/libs/validate";
    import moment from "moment/moment";
    export default {
        name: 'addEdit',
        components: {
            AFormItem,
        },
        data () {
            return {
                verify,
                form: this.$form.createForm(this),
                showAddTable: false,
                input: false,
              useState: '1',
              entryTime: '',
              receiveTime: '',
              orderId: '',
              orderCode: '',
              orderName: '',
              labId: '',
              labObj: {},
              orderObj: {},
            }
        },
        computed: {
          orderSelectList () {
            return this.$store.state.orderManage.orderSelectList
          },
          labSelectList () {
            return this.$store.state.acManage.labSelectList
          },
            formStyle() {
                return this.$store.state.common.formStyle
            }
        },
        props: {

            title: {
                type: Boolean,
                required: true
            },
          objDTO:{
            type: Object,
            required: true
          }
        },
        watch: {

        },
        methods: {
          validateID,
            /**
             * Function    : saveApply
             * Description : 打开弹框
             * Input       : none
             * Return      : none
             */
            showModal () {
                this.showAddTable = true
            },
            /**
             * Function    : showEdit
             * Description : 编辑时显示
             * Input       : opt单个所有数据
             * Return      : none
             */
            showEdit ( opt ) {
                this.showAddTable = true
                this.id = opt.recordId
                this.$nextTick (() => {
                    this.form.setFieldsValue (this.$pick (opt, [ 'orderCode', 'orderName', 'receiveTime','location','useState','entryPersion','entryTime' ]))
                })
              this.pickOrderSelect(opt.orderCode)

            },

          pickLabSelect(location) {
              let labSelectList = this.labSelectList;
              for (let index in labSelectList){
                let labObj = labSelectList[index].record;
                if (labObj.name === location ){
                  this.labId = labObj.recordId
                  this.location = labObj.name
                  this.$nextTick (() => {
                    this.form.setFieldsValue (this.$pick ({labId:this.labId}, [ 'labId' ]))
                  })
                }
              }
          },
          pickOrderSelect(orderCode) {
              let orderSelectList = this.orderSelectList;
              for (let index in orderSelectList){
                let orderObj = orderSelectList[index].record;
                if (orderObj.orderCode === orderCode ){
                  this.orderId = orderObj.recordId
                  this.orderCode = orderObj.orderCode
                  this.orderName = orderObj.name
                  this.$nextTick (() => {
                    this.form.setFieldsValue (this.$pick ({orderId:this.orderId}, [ 'orderId' ]))
                  })
                }
              }
          },

            /**
             * Function    : saveApply
             * Description : 角色信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply ( ) {
                this.form.validateFields((err, value) => {
                    if (!err) {
                        let para = {}
                        para.orderCode = this.orderCode
                        para.orderName = this.orderName
                        para.receiveTime = moment (value.receiveTime).format ('YYYY-MM-DD HH:MM:SS')
                        para.location = value.location
                        para.useState = this.useState
                        para.entryPersion = value.entryPersion
                        para.entryTime = moment (value.entryTime).format ('YYYY-MM-DD HH:MM:SS')
                      if (!this.title){
                        para.recordId = this.id
                      }
                      this.$store.dispatch ('saveEntryAct', para).then (response => {
                          this.showAddTable = false
                          if (response.code === 0) {
                              this.$emit ('init')
                              this.$message.success (response.message)
                          } else {
                              this.$message.error (response.message)
                          }
                      })
                    }
                })
            },
          changeOrderList ( value ) {
            let _this = this;
            let orderSelectList = _this.orderSelectList;
            for (let index in orderSelectList){
              if (orderSelectList[index].value === value){
                let orderObj = orderSelectList[index].record
                this.orderCode = orderObj.orderCode
                this.orderName = orderObj.name
                this.orderObj = orderObj;
              }
            }
          },
          changeLabList ( value ) {
            let _this = this;
            let labSelectList = _this.labSelectList;
            for (let index in labSelectList){
              if (labSelectList[index].value === value){
                let labObj = labSelectList[index].record
                this.location = labObj.name
                this.labObj = labObj;
              }
            }
          },

            /**
             * Function    : cancelApply
             * Description : 取消弹出框
             * Input       : val
             * Return      : none
             */
            cancelApply ( ) {
                this.showAddTable = false
                let _this = this
                setTimeout(function() {
                    _this.form.resetFields()
                }, 100)
            },
          initData() {
            this.$store.dispatch ('getOrderListAct', {})
            this.$store.dispatch ('getLabListAct', {})
          }
        },

        created () {
          this.initData()
        }
    }
</script>
